package com.tianxin.workbao.workBao.fragment;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.CoalChemicalAdapter;
import com.tianxin.workbao.workBao.bean.CtCoaltype;
import com.tianxin.workbao.workBao.bean.Ctchemical;
import com.tianxin.workbao.workBao.dataProvide.CoalChemicalProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class CoalChemicalFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, UpdateListViewListener {

    private Context mContent;
    private int page = 1;
    private boolean isEnd = false;//是否到最后的数据
    private CoalChemicalAdapter coalChemicalAdapter;
    private CoalChemicalProvide coalChemicalProvide;
    private List<Ctchemical> ctchemicalList;
    private CtCoaltype mCtCoaltype;
    private boolean isFromTest;

    public static CoalChemicalFragment newInstance(CtCoaltype coaltype) {

        Bundle args = new Bundle();
        args.putSerializable("ctCoalType", coaltype);
        CoalChemicalFragment fragment = new CoalChemicalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContent = getContext();
        mCtCoaltype = (CtCoaltype) getArguments().getSerializable("ctCoalType");
        ctchemicalList = new ArrayList<>();
        this.isFromTest = getArguments().getBoolean("form");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void initRootView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.recircle_view_content, container, false);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) mRootView.findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mSwipeRefreshLayout.setColorSchemeResources(
                R.color.recycler_color1,
                R.color.recycler_color2,
                R.color.recycler_color3,
                R.color.recycler_color4);

        coalChemicalAdapter = new CoalChemicalAdapter(ctchemicalList, getContext());
        mRecyclerView.setAdapter(coalChemicalAdapter);
        //设置布局管理器
        mLayoutManager = new GridLayoutManager(getContext(), spanCount);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return coalChemicalAdapter.getItemColumnSpan(position);
            }
        });
        mRecyclerView.setLayoutManager(mLayoutManager);

        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initData(boolean isSavedNull) {
        onRefresh();
    }

    @Override
    public void initEvents() {
        mRecyclerView.addOnScrollListener(new RecyclerViewScrollListener());
    }

    @Override
    public void search(String query) {
        showMessage(getResources().getString(R.string.developing));
    }

    //SwipeRefreshLayout.OnRefreshListener
    @Override
    public void onRefresh() {
        if (mSwipeRefreshLayout.isRefreshing() == false) {
            mSwipeRefreshLayout.setRefreshing(true);
            getCoalPriceProvide().getdatas(mCtCoaltype.getContractid(), mCtCoaltype.getCoaltypeid(), page = 1);
        }
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(String msg) {
        ToastUtils.showShort(msg);
    }

    @Override
    public void showProgress() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideProgress() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefreshData(Object result) {
        ctchemicalList.clear();
        if (result instanceof List) {
            ctchemicalList.addAll((List<Ctchemical>) result);
        }
        coalChemicalAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadMoreData(Object result) {
        if (result instanceof List) {
            if (result == null) {
                isEnd = true;
            } else {
                ctchemicalList.addAll((List<Ctchemical>) result);
                coalChemicalAdapter.notifyDataSetChanged();
            }
        }
    }


    class RecyclerViewScrollListener extends RecyclerView.OnScrollListener {

        private int lastVisibleItem;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == coalChemicalAdapter.getItemCount()) {
                onLoadMore();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
        }
    }

    //加载更多
    private void onLoadMore() {
        if (isEnd) {
            mSwipeRefreshLayout.setRefreshing(false);
            showMessage(getResources().getString(R.string.no_data));
        } else {
            if (!mSwipeRefreshLayout.isRefreshing()) {
                page++;
                getCoalPriceProvide().getdatas(mCtCoaltype.getContractid(), mCtCoaltype.getCoaltypeid(), page);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    }

    public CoalChemicalProvide getCoalPriceProvide() {
        if (coalChemicalProvide == null) {
            coalChemicalProvide = new CoalChemicalProvide(getContext(), this);
        }
        return coalChemicalProvide;
    }
}
